function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);//search,查询？后面的参数，并匹配正则
    if (r != null) return unescape(r[2]);
    return null;
}

var Type = GetQueryString("Type");
function JobList() {
    $.ajaxSettings.async = false;
    $("#question_table").bootstrapTable('destroy');
    $('#question_table').bootstrapTable({
        url: "/jenkins/job/list",
        method: 'post',
        contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
        cache: false,
        queryParams: queryParams,
        striped: true,
        pagination: true,
        sortable: true,
        sortOrder: "desc",
        sortName: "posttime",
        sidePagination: 'client',
        pageSize: 10,
        pageNumber: 1,
        pageList: [10, 20, 50, 100, 200, 500],
        search: true,
        strictSearch: false,
        showColumns: true,
        showRefresh: false,
        showExport: false,
        uniqueId: "number",
        showToggle: false,
        cardView: false,
        // clickToSelect: true,
        //  showFooter:true,
        columns:
            [
                // {field: "checked", checkbox: true},
                {
                    field: "number",
                    align: "center",
                    valign: "middle",
                    title: "序号",
                    width: 60,
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                },
                {
                    field: "color",
                    title: "当前状态",
                    align: "center",
                    width: 60,
                    valign: "middle",
                    sortable: "true",
                    formatter: NowStatus
                },
                {
                    field: "name",
                    title: "job名称",
                    align: "left",
                    width: 250,
                    valign: "middle",
                    sortable: "true",
                    formatter: function (value, row, index) {
                        return ['<a title="\'' + row.detail + '\'">' + value + '</a>'].join("");
                    }
                },
                {
                    title: "构建",
                    align: "center",
                    valign: "middle",
                    width: 60,
                    sortable: "true",
                    formatter: buildJob
                },

                {field:"inqueue",align: "center", valign: "middle",width:60, sortable: "true",visible: false },

                {
                    field: "url",
                    title: "跳转到Job",
                    align: "center",
                    width: 80,
                    valign: "middle",
                    sortable: "true",
                    formatter: LinkTest
                },
                {field: "lastBuild", title: "上一次构建", align: "center", valign: "middle", sortable: "true"},
                {field: "lastResult", title: "上一次构建结果", align: "center", valign: "middle", sortable: "true"},
                {
                    field: "lastSuccessfulBuild",
                    title: "上一次成功的构建",
                    align: "center",
                    valign: "middle",
                    sortable: "true",
                    visible: false
                },
                {
                    field: "lastFailedBuild",
                    title: "上一次失败的构建",
                    align: "center",
                    valign: "middle",
                    sortable: "true",
                    visible: false
                },
                {
                    field: "detail",
                    align: "center",
                    valign: "middle",
                    sortable: "true",
                    visible: false
                },

            ],
        onLoadSuccess: function (data) {
            if (data.code != null) {
                var code = data.code;
                var message = data.message;
                if (code == "400") {
                    alert(message);
                    window.parent.location.href = "/login.html";
                }
                else if (code == "401") {
                    alert(message);
                    window.location.href = "/error/error.html";
                }
            }
        }


    });

    function LinkTest(value) {
        return ['<input title="跳转到Jenkins" type="image" src="../../images/icon/headshot.png" style="icon: auto" onclick="JumpTo(\'' + value + '\')"/>'].join("");
    }

    function NowStatus(value, row, index) {
        if (row.inqueue=="true") return ['<input title="排队中" type="image" src="../../images/icon/waiting.gif">'].join("");
        else {
        }
        if (value == "aborted_anime") return ['<input title="正在构建" type="image" src="../../images/icon/aborted_anime.gif" style="icon: auto" >'].join("");
        else if (value == "red_anime") return ['<input title="正在构建" type="image" src="../../images/icon/red_anime.gif" style="icon: auto" >'].join("");
        else if (value == "blue_anime") return ['<input title="正在构建" type="image" src="../../images/icon/blue_anime.gif" style="icon: auto" >'].join("");
        else if (value == "blue") return ['<input title="构建成功" type="image" src="../../images/icon/blue.png" style="icon: auto" >'].join("");
        else if (value == "aborted") return ['<input title="构建中断" type="image" src="../../images/icon/aborted.png" style="icon: auto" >'].join("");
        else if (value == "red") return ['<input title="构建失败" type="image" src="../../images/icon/red.png" style="icon: auto" >'].join("");
    }
}

function buildJob(value, row, index) {
    if(row.color=="blue_anime"||row.color=="aborted_anime"||row.inqueue=="true")return [
        '<input title="停止构建" type="image" src="../../images/icon/stop.png" style="icon: auto" onclick="StopNow(\'' + row.name + '\')">'
    ].join("");
    return [
        '<input title="立即构建" type="image" src="../../images/icon/clock.png" style="icon: auto" onclick="BuildNow(\'' + row.name + '\')">'
    ].join("");
}


function queryParams(params) {
    return {
        type: Type,
    }
}

function BuildNow(jobName) {
    alert("操作已申请，Jenkins后台有延迟 请勿重复提交!");
    $.ajax({
        url: "/jenkins/job/build", //处理页面的路径
        data: {jobName: jobName},//要提交的数据是一个JSON
        type: "POST", //提交方式
        async:false,
        //TEXT字符串 JSON返回JSON XML返回XML
        success: function (data) { //回调函数 ,成功时返回的数据存在形参data里
            if (data == false) alert("执行失败!");
            else if (data == true) setTimeout("JobList()",20000);
            else alert(data);
            //location.reload();
        }
    });
}

function StopNow(jobName) {
    alert("操作已申请，Jenkins后台有延迟 请勿重复提交!");
    $.ajax({
        url: "/jenkins/job/stop", //处理页面的路径
        data: {jobName: jobName},//要提交的数据是一个JSON
        type: "POST", //提交方式
        async:false,
        //TEXT字符串 JSON返回JSON XML返回XML
        success: function (data) { //回调函数 ,成功时返回的数据存在形参data里
            if (data == false)alert("执行失败!");
            else if (data == true)setTimeout("JobList()",200000);
            else alert(data);
            //location.reload();
        }
    });
}

function JumpTo(url) {
    window.parent.location.href = url;
}